package com.dom.domweb.modular.shiro;

import com.dom.domweb.dao.entity.TRoleInfo;
import com.dom.domweb.dao.entity.TUserInfo;
import com.dom.domweb.dao.entity.TUserRoleRel;
import com.dom.domweb.dao.service.ITRoleInfoService;
import com.dom.domweb.dao.service.ITUserInfoService;
import com.dom.domweb.dao.service.ITUserRoleRelService;
import com.dom.domweb.modular.shiro.model.ShiroUser;
import com.dom.domweb.modular.shiro.service.UserAuthService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 自定义的shiro认证
 * @Author xllei
 * @Date 2018/11/16 6:13 PM
 * @Version 1.0
 **/
public class MyShiroRealm extends AuthorizingRealm {
    @Autowired
    private ITUserInfoService userInfoService;
    @Autowired
    private ITRoleInfoService roleInfoService;
    @Autowired
    private ITUserRoleRelService userRoleRelService;

    /**
     * 用户认证
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获得当前用户的用户名
        String username = (String) authenticationToken.getPrincipal();
//
//        //从数据库中根据用户名查找用户
//        TUserInfo user = userInfoService.getUserByUserName(username);
//        if (user == null) {
//            throw new UnknownAccountException("没有在本系统中找到对应的用户信息");
//        }
//        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getUserName(), user.getPasswd(), getName());

        UserAuthService userAuthService = UserAuthService.getInstance();

        TUserInfo userInfo = userAuthService.getUser(username);

        ShiroUser shiroUser = userAuthService.getShiroUser(userInfo);

        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(shiroUser, userInfo.getPasswd(), getName());
        return info;
    }

    /**
     * 用户授权
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //从凭证中获得用户名
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        //根据用户名查询用户对象
        TUserInfo user = userInfoService.getUserByUserName(username);
        //查询用户拥有的角色
        List<TUserRoleRel> roleRels = userRoleRelService.findByUserId(user.getId());
        List<Integer> roles = roleRels.stream().map(TUserRoleRel::getRoleId).collect(Collectors.toList());
        List<TRoleInfo> list = roleInfoService.selectBatchIds(roles);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        for (TRoleInfo role : list) {
            //赋予用户角色
            info.addStringPermission(role.getRoleName());
        }
        return info;
    }


}
